#ifndef PLAYER_H
#define PLAYER_H

#include <iostream>

#include "entity.h"
#include "targa.h"
#include "md2model.h"

class MD2Model;

enum PlayerState{
    PLAYER_IDLE,
    PLAYER_RUN,
};

class Player : public Entity
{
    public:
		static const int PLAYER_MODE = 0;
		static const int FREE_MODE = 1;
		static const int NUM_KEYS = 4;
        /** Default constructor */
        Player(GameWorld* const world);
		virtual bool isStatic() { return false; }
        /** Default destructor */
        virtual ~Player();
        /** Access m_score
         * \return The current value of m_score
         */
        unsigned int getScore() const { return m_score; }
        /** Set m_score
         * \param val New value to set
         */
        void getScore(unsigned int val) { m_score = val; }

        virtual EntityType getType() const { return PLAYER; }

        //At the moment the player doesn't collide with other entities
        virtual Collider* getCollider() { return m_collider; }

		int getMode();

        Vector3 getPosition() const { return m_position; }
        void setPosition(const Vector3& pos) {
            m_position = pos;
        }

        //Vector3 getVelocity() const { return m_velocity; }

        float getYaw() const { return m_yaw; }
        float getPitch() const { return m_pitch; }
        void setYaw(const float yaw) { m_yaw = yaw; }
        void setPitch(const float pitch) { m_pitch = pitch; }

        void yaw(const float val);
        void pitch(const float val);
        void moveForward(const float speed);
		void moveForwardFreeMode(const float speed);
        void increaseScore(int amount) { m_score += amount; }

		int getViewMode() { return m_viewMode; }		
		
		int m_selectedVertex;
		int m_selectedClump;

		bool* getKeys() { return &m_keys[0]; }
		int getNumKeys() { return NUM_KEYS; }
    private:
        unsigned int m_score; //!< Member variable "m_score"
		int mode;
		int m_viewMode;
		void Player::convertTo3d(int x, int y, Vector3* out);
		void getClick(Vector3* loc, Vector3* dir);
		void calculateRay(Vector3* dir, Vector3* loc);
        virtual void onPrepare(float);
        virtual void onRender() const;
        virtual void onPostRender();
        virtual bool onInitialize();
        virtual void onShutdown();
        virtual void onCollision(Entity* collider); //Players don't collide.. yet

		bool rayInSphere(Vertex v, float radius, Vector3* origin, Vector3* dir);
		float pointToRay(Vertex* v, Vector3* o, Vector3* dir);

        Vector3 m_position;
        //Vector3 m_velocity;

        float m_yaw;
        float m_pitch;
        Collider* m_collider;

		PlayerState m_pState;
		MD2Model* m_model;
        TargaImage m_playerTexture;
        unsigned int m_playerTextureID;
		
		GameWorld* m_world;

		int m_lastMouseX;
		int m_lastMouseY;

		bool m_keys[NUM_KEYS];

};

#endif // PLAYER_H
